package p.sglmsn.top.ext.wx.handle;

import cn.hutool.core.util.XmlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.w3c.dom.Element;
import p.sglmsn.top.ext.wx.enums.MethodEnum;
import p.sglmsn.top.ext.wx.iface.EventInterface;
import p.sglmsn.top.ext.wx.iface.WeCharService;
import p.sglmsn.top.ext.wx.wxext.http.SendDto;

import java.util.concurrent.ConcurrentHashMap;

@Component
public class WxMsgHandle {


    public static final ConcurrentHashMap<Integer, EventInterface> interfaceConcurrentHashMap = new ConcurrentHashMap<>(512);
    @Autowired
    WeCharService weCharService;


    public WxMsgHandle() {

        add(informationReceived -> {
            weCharService.receivedTheNews(informationReceived);
            return null;
        }, 1, 3, 34, 43, 47, 48, 49, 10000); //收到消息
        add(informationReceived -> {
            weCharService.receivedTheNews(informationReceived);
            return null;
        }, 42); //名片
        add(informationReceived -> {
            Element documentElement = XmlUtil.parseXml(informationReceived.getJSONObject("data").getString("msg")).getDocumentElement();
            String encryptusername = documentElement.getAttribute("encryptusername");
            String fromid = documentElement.getAttribute("fromusername");
            String ticket = documentElement.getAttribute("ticket");
            MethodEnum.接受好友请求.exec(new SendDto().setEncryptusername(encryptusername).setTicket(ticket));

            return null;
        }, 37); //收到好友请求


        add(informationReceived -> {
            weCharService.receivedTransfer(informationReceived);
            return null;
        }, 49);  //转账
        add(informationReceived -> {

            return null;
        }, 701); //群成员更新
        add(informationReceived -> {

            return null;
        }, 702); //群成员增加
        add(informationReceived -> {

            return null;
        }, 703); //群成员减少
        add(informationReceived -> {

            return null;
        }, 704); //联系人更新
        add(informationReceived -> {

            return null;
        }, 705); //收款结果
        add(informationReceived -> {

            return null;
        }, 706); //好友验证结果
        add(informationReceived -> {

            return null;
        }, 708); // 对应xml图片地址
        add(informationReceived -> {
            weCharService.login(informationReceived);
            return null;
        }, 724); // 登录

    }

    public static EventInterface get(Integer type) {
        return interfaceConcurrentHashMap.get(type);
    }

    void add(EventInterface eventInterface, Integer... types) {
        for (Integer integer : types) {
            interfaceConcurrentHashMap.put(integer, eventInterface);
        }
    }
}
